package com.quanyan.stadium.service.commonComponent.impl;

import com.quanyan.common.utils.GsonUtils;
import com.quanyan.common.utils.HttpUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.stadium.entity.vo.*;
import com.quanyan.stadium.service.commonComponent.IMService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zheng.Ke
 * Date 2016/7/9.
 */
@Service
public class IMServiceImpl implements IMService {
    private static Logger logger = LoggerFactory.getLogger(IMServiceImpl.class);

    @Value("${im.base.url}")
    private String imBaseURL;

    @Override
    public IMResponseGroupVo createIMGroup(CreateIMGroupVo createIMGroupVo) {
        String url = imBaseURL + "/createGroup.api";
        String responStr = HttpUtils.sendWebRequestByPostJsonStr(null, url, GsonUtils.toJson(createIMGroupVo));
        IMResponseGroupVo vo = new IMResponseGroupVo();
        if (StringUtil.isBlankOrNull(responStr)) {
            vo.setErrcode(-1);
            vo.setRet(false);
            vo.setErrmsg("调用C++端创建群组接口返回为空。");
            logger.warn("创建群组出错。请求：{}，晌应：{}。", GsonUtils.toJson(createIMGroupVo), responStr);
            return vo;
        }
        vo = GsonUtils.jsonStrToEntity(responStr, IMResponseGroupVo.class);
        return vo;
    }


    @Override
    public IMResponseGroupVo addMembers(String groupid, List<Integer> uidList, int currentUid) {
        String url = imBaseURL + "/addMembers.api";
        AddMembersVo addMembersVo = new AddMembersVo();
        addMembersVo.setGroupid(groupid);
        addMembersVo.setMembers(uidList);
        addMembersVo.setCurrentUid(currentUid);
        String responseStr = HttpUtils.sendWebRequestByPostJsonStr(null, url, GsonUtils.toJson(addMembersVo));
        IMResponseGroupVo vo = new IMResponseGroupVo();
        if (StringUtil.isBlankOrNull(responseStr)) {
            vo.setErrcode(-1);
            vo.setRet(false);
            vo.setErrmsg("调用C++端增加群组成员接口返回为空。");
            logger.warn("增加群组成员出错。groupid：{}，uidList：{}，currentUid：{}，晌应：{}。", groupid, GsonUtils.toJson(uidList), currentUid, responseStr);
            return vo;
        }
        vo = GsonUtils.jsonStrToEntity(responseStr, IMResponseGroupVo.class);
        return vo;
    }

    @Override
    public IMResponseGroupVo addMembers(String groupid, int uid, int currentUid) {
        List<Integer> uidList = new ArrayList<Integer>();
        uidList.add(uid);
        return addMembers(groupid, uidList, currentUid);
    }

    @Override
    public IMResponseGroupVo deleteMembers(Integer currentUid, String groupid, int deleteUid, int extid) {
        String url = imBaseURL + "/deleteMembers.api";
        List<Integer> uidList = new ArrayList<Integer>();
        uidList.add(deleteUid);

        DeleteMembersVo deleteMembersVo = new DeleteMembersVo();
        deleteMembersVo.setGroupid(groupid);
        deleteMembersVo.setMembers(uidList);
        deleteMembersVo.setCurrentUid(currentUid);
        deleteMembersVo.setExtid(extid);

        String responseStr = HttpUtils.sendWebRequestByPostJsonStr(null, url, GsonUtils.toJson(deleteMembersVo));
        IMResponseGroupVo vo = new IMResponseGroupVo();
        if (StringUtil.isBlankOrNull(responseStr)) {
            vo.setErrcode(-1);
            vo.setRet(false);
            vo.setErrmsg("调用C++端删除群组成员接口返回为空。");
            logger.warn("删除群组成员出错。请求{}，晌应：{}。", GsonUtils.toJson(deleteMembersVo), responseStr);
            return vo;
        }
        vo = GsonUtils.jsonStrToEntity(responseStr, IMResponseGroupVo.class);
        return vo;
    }

    @Override
    public IMResponseGroupVo quitGroup(Integer currentUid, String groupid, int extid) {
        String url = imBaseURL + "/quitGroup.api";

        QuitGroupVo quitGroupVo = new QuitGroupVo();
        quitGroupVo.setGroupid(groupid);
        quitGroupVo.setCurrentUid(currentUid);
        quitGroupVo.setExtid(extid);

        String responseStr = HttpUtils.sendWebRequestByPostJsonStr(null, url, GsonUtils.toJson(quitGroupVo));
        IMResponseGroupVo vo = new IMResponseGroupVo();
        if (StringUtil.isBlankOrNull(responseStr)) {
            vo.setErrcode(-1);
            vo.setRet(false);
            vo.setErrmsg("调用C++端退群接口返回为空。");
            logger.warn("退群出错。请求{}，晌应：{}。", GsonUtils.toJson(quitGroupVo), responseStr);
            return vo;
        }
        vo = GsonUtils.jsonStrToEntity(responseStr, IMResponseGroupVo.class);
        return vo;
    }
}
